//
//  DDSuccessViewController.m
//  PrivateCloud
//
//  Created by sarah doo on 13-10-14.
//  Copyright (c) 2013年 sarah doo. All rights reserved.
//

#import "DDSuccessViewController.h"
#import "UIColor+.h"
#import "DDAppDelegate.h"
#import "DDSeverViewController.h"
#import "DDSeverSubViewController.h"
#import "AGImagePickerController.h"
#import "AGIPCToolbarItem.h"
#import "DDLittleViewController.h"

#import "DDVerifyThread.h"
#import "DDConstants.h"
#import "DDContactInfosView.h"
#import "MBProgressHUD.h"
#import "TestAddressBook.h"
#import "AppUtil.h"
#import "DDAddressBookUtils.h"
#import "DDTCPListenThread.h"
#import "DDLoginViewController.h"
#import "DDImagePickerController.h"

#define IMAGE_TAG 1
#define MUSIC_TAG 2
#define FILE_TAG 3
#define VIDEO_TAG 4
#define CONTACT_TAG 5

@interface DDSuccessViewController (){
    AGImagePickerController *ipc;
}
@end
//MARS 登陆成功界面
@implementation DDSuccessViewController
@synthesize selectedPhotos;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    self.selectedPhotos = [NSMutableArray array];
    
    //    DDSuccessViewController *blockSelf = self;
    
    NSUserDefaults* userDefaults = [[NSUserDefaults alloc]init];
    NSString* cloudName = [userDefaults objectForKey:SERVER_CLOUD_NAME_KEY];
    
    
    //    ipc = [[AGImagePickerController alloc] initWithDelegate:self];
    //    ipc.didFailBlock = ^(NSError *error) {
    //        NSLog(@"Fail. Error: %@", error);
    //
    //        if (error == nil) {
    //            [blockSelf.selectedPhotos removeAllObjects];
    //            NSLog(@"User has cancelled.");
    //
    //            [blockSelf dismissModalViewControllerAnimated:YES];
    //        } else {
    //
    //            // We need to wait for the view controller to appear first.
    //            double delayInSeconds = 0.5;
    //            dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    //            dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    //                [blockSelf dismissModalViewControllerAnimated:YES];
    //            });
    //        }
    //
    //        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
    //
    //    };
    //    ipc.didFinishBlock = ^(NSArray *info) {
    //        [blockSelf.selectedPhotos setArray:info];
    //
    //        NSLog(@"Info: %@", info);
    //        [blockSelf dismissModalViewControllerAnimated:YES];
    //
    //        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
    //    };
    
    [super viewDidLoad];
    [self createStaticViewWithCloudName:cloudName];
}

- (void)createStaticViewWithCloudName:(NSString*)cloudName{
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 40)];
    
    topView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:topView];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2 - 100, 5, 20, 20)];
    [imageView setImage:[UIImage imageNamed:@"button_internet.png"]];
    [topView addSubview:imageView];
    [imageView release];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2 - 50, 0, 200, 30)];
    titleLabel.text = @"无 忧 易 云";
    titleLabel.font = [UIFont systemFontOfSize:15];
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.backgroundColor = [UIColor clearColor];
    //    [topView setBackgroundColor:[UIColor colorWithString:@"#275725"]];
    [topView addSubview:titleLabel];
    [titleLabel release];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(self.view.frame.size.width - 80, 10, 60, 40);
    //    [button setTitle:@"设 置" forState:UIControlStateNormal];
    //    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(setButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    UILabel *buttonLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, button.frame.size.width/2, button.frame.size.height/2)];
    buttonLabel.textColor = [UIColor whiteColor];
    buttonLabel.font = [UIFont systemFontOfSize:23];
    buttonLabel.backgroundColor = [UIColor clearColor];
    //    buttonLabel.text = @"设置";
    [button addSubview:buttonLabel];
    [buttonLabel release];
    [topView addSubview:button];
    [topView release];
    
    UIView *linkView = [[UIView alloc] initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, 40)];
    linkView.backgroundColor = [UIColor colorWithString:@"#273826"];
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *image = [UIImage imageNamed:@"ico_back.png"];
    backButton.frame = CGRectMake(10, 4, 32, 32);
    [backButton setImage:image forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backbuttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [linkView addSubview:backButton];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 5, self.view.frame.size.width - image.size.width , 30)];
    label.font = [UIFont systemFontOfSize:15];
    NSString* str1 = @"与设备";
    NSString* str2 = @"同步内容";
    NSString* titleString = [NSString stringWithFormat:@"%@%@%@",str1,cloudName,str2];
    label.text = titleString;
    label.textColor = [UIColor whiteColor];
    [linkView addSubview:label];
    [label release];
    [self.view addSubview:linkView];
    [linkView release];
    
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 90, self.view.frame.size.width, self.view.frame.size.height - 80)];
    [bgView setBackgroundColor:[UIColor colorWithString:@"#275725"]];
    [self.view addSubview:bgView];
    
    severButton = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *images = [UIImage imageNamed:@"server.png"];
    severButton.frame = CGRectMake(self.view.frame.size.width/2-70, 50, images.size.width/2, images.size.height/2);
    [severButton addTarget:self action:@selector(severButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [severButton setImage:images forState:UIControlStateNormal];
    [bgView addSubview:severButton];
    
    UIButton *imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *image2 = [UIImage imageNamed:@"picture.png"];
    imageButton.tag = IMAGE_TAG;
    [imageButton addTarget:self action:@selector(littleButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    imageButton.frame = CGRectMake(17, 344, 40, 40);
    [imageButton setImage:image2 forState:UIControlStateNormal];
    [bgView addSubview:imageButton];
    
    UIButton *videoButton = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *image3 = [UIImage imageNamed:@"video.png"];
    videoButton.tag = VIDEO_TAG;
    [videoButton addTarget:self action:@selector(littleButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    videoButton.frame = CGRectMake(128-image3.size.width/2 - 30, 284, 40, 40);
    [videoButton setImage:image3 forState:UIControlStateNormal];
    [bgView addSubview:videoButton];
    
    UIButton *musicButton = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *image4 = [UIImage imageNamed:@"music.png"];
    musicButton.tag = MUSIC_TAG;
    [musicButton addTarget:self action:@selector(littleButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    musicButton.frame = CGRectMake(128-image3.size.width/2 + 50, 234, 40, 40);
    [musicButton setImage:image4 forState:UIControlStateNormal];
    [bgView addSubview:musicButton];
    
    
    UIButton *icoButton = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *image5 = [UIImage imageNamed:@"ico_6.png"];
    icoButton.frame = CGRectMake(128-image3.size.width/2 + 130, 284, 40, 40);
    icoButton.tag = FILE_TAG;
    [icoButton addTarget:self action:@selector(littleButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [icoButton setImage:image5 forState:UIControlStateNormal];
    [bgView addSubview:icoButton];
    
    UIButton *contactButton = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *image6 = [UIImage imageNamed:@"contact.png"];
    contactButton.frame = CGRectMake(128-image3.size.width/2 + 180, 344, 40, 40);
    contactButton.tag = CONTACT_TAG;
    [contactButton addTarget:self action:@selector(littleButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    
    [contactButton setImage:image6 forState:UIControlStateNormal];
    [bgView addSubview:contactButton];
    
    UIImageView *musicView = [[UIImageView alloc] initWithFrame:CGRectMake(128-image3.size.width/2 + 50, 304, 44, 85)];
    [musicView setImage:[UIImage imageNamed:@"phone.png"]];
    [bgView addSubview:musicView];
    [musicView release];
    
    [bgView release];
}

- (void)setButtonPressed:(id)sender {
    
}

- (void)littleButtonPressed:(id)sender {
    UIButton *button = (UIButton *)sender;
    int index = -1;
    if (button.tag != CONTACT_TAG) {
        if (button.tag == MUSIC_TAG) {
            index = 1;
        }
        else if(button.tag == IMAGE_TAG){
            index = 0;
        }
        else if(button.tag == VIDEO_TAG){
            index = 2;
        }
        else if(button.tag == FILE_TAG){
            index = 3;
        }
        //用于出来除了图片之外的内容
        if(button.tag != IMAGE_TAG){
            DDSeverSubViewController* serverSub = [[DDSeverSubViewController alloc]initWithNibName:nil bundle:nil];
            serverSub.from = 1;
            //FIX ME 测试用的
            serverSub.type = LOCAL_CODE;
            DDFolderInfo* folderInfo = [[DDConstants shardLocalFolderInfos]objectAtIndex:index];
            serverSub.fileDataSource = [folderInfo files];
            
            [self presentViewController:serverSub animated:NO completion:nil];
            [serverSub release];
        }
        else{
            DDImagePickerController* imageController = [[DDImagePickerController alloc]initWithNibName:nil bundle:nil];
            imageController.fileDataSource = [[[DDConstants shardLocalFolderInfos]objectAtIndex:0]files];
            
            [self presentViewController:imageController animated:NO completion:nil];
            [imageController release];
        }
    }
    else{
        //开启进度条
        [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            TestAddressBook* test = [[TestAddressBook alloc]init];
            //            NSString* localVcfPath = [NSString stringWithFormat:@"%@%@",[AppUtil getDocPath],@"local.vcf"];
            DDAddressBookUtils* bookUtils = [[DDAddressBookUtils alloc]init];
            [bookUtils createVcfFileWithCode:LOCAL_CODE];
            [bookUtils createVcard];
            
            NSMutableArray* contactInfos = [test loadVCF:[bookUtils getLocalVcfPath]];
            dispatch_async(dispatch_get_main_queue(), ^{
                [MBProgressHUD hideHUDForView:self.view animated:YES];
                DDContactInfosView* contactView = [[DDContactInfosView alloc]initWithNibName:nil bundle:nil];
                contactView.fileDataSource = contactInfos;
                contactView.type = LOCAL_CODE;
                [self presentViewController:contactView animated:NO completion:nil];
                [contactView release];
            });
        });
        //DDContactInfosView* contactView = [[DDContactInfosView alloc]initWithNibName:nil bundle:nil];
    }
    
    
}

- (void)openImages {
    //    AGImagePickerController *imagePickerController = [[AGImagePickerController alloc] initWithFailureBlock:^(NSError *error) {
    //        NSLog(@"Fail. Error: %@", error);
    //
    //        if (error == nil) {
    //            [self.selectedPhotos removeAllObjects];
    //            NSLog(@"User has cancelled.");
    //            [self dismissViewControllerAnimated:YES completion:nil];
    //        } else {
    //
    //            // We need to wait for the view controller to appear first.
    //            double delayInSeconds = 0.5;
    //            dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    //            dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    //                [self dismissViewControllerAnimated:YES completion:nil];
    //            });
    //        }
    //
    //        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
    //
    //    } andSuccessBlock:^(NSArray *info) {
    //        [self.selectedPhotos setArray:info];
    //
    //        NSLog(@"Info: %@", info);
    //       // [self dismissModalViewControllerAnimated:YES];
    //        [self dismissViewControllerAnimated:YES completion:nil];
    //        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
    //    }];
    //
    //    // Show saved photos on top
    //    imagePickerController.shouldShowSavedPhotosOnTop = YES;
    //    imagePickerController.selection = self.selectedPhotos;
    //
    //    // Custom toolbar items
    //    AGIPCToolbarItem *selectAll = [[AGIPCToolbarItem alloc] initWithBarButtonItem:[[[UIBarButtonItem alloc] initWithTitle:@"+ Select All" style:UIBarButtonItemStyleBordered target:nil action:nil] autorelease] andSelectionBlock:^BOOL(NSUInteger index, ALAsset *asset) {
    //        return YES;
    //    }];
    //    AGIPCToolbarItem *flexible = [[AGIPCToolbarItem alloc] initWithBarButtonItem:[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease] andSelectionBlock:nil];
    //    AGIPCToolbarItem *selectOdd = [[AGIPCToolbarItem alloc] initWithBarButtonItem:[[[UIBarButtonItem alloc] initWithTitle:@"+ Select Odd" style:UIBarButtonItemStyleBordered target:nil action:nil] autorelease] andSelectionBlock:^BOOL(NSUInteger index, ALAsset *asset) {
    //        return !(index % 2);
    //    }];
    //    AGIPCToolbarItem *deselectAll = [[AGIPCToolbarItem alloc] initWithBarButtonItem:[[[UIBarButtonItem alloc] initWithTitle:@"- Deselect All" style:UIBarButtonItemStyleBordered target:nil action:nil] autorelease] andSelectionBlock:^BOOL(NSUInteger index, ALAsset *asset) {
    //        return NO;
    //    }];
    //    imagePickerController.toolbarItemsForSelection = [NSArray arrayWithObjects:selectAll, flexible, selectOdd, flexible, deselectAll, nil];
    //    //    imagePickerController.toolbarItemsForSelection = [NSArray array];
    //    [selectOdd release];
    //    [flexible release];
    //    [selectAll release];
    //    [deselectAll release];
    //
    //    //    imagePickerController.maximumNumberOfPhotos = 3;
    //    [self presentViewController:imagePickerController animated:YES completion:nil];
    //    [imagePickerController release];
    // Show saved photos on top
//    ipc.shouldShowSavedPhotosOnTop = NO;
//    ipc.shouldChangeStatusBarStyle = YES;
//    ipc.selection = self.selectedPhotos;
//    //    ipc.maximumNumberOfPhotosToBeSelected = 1;
//    
//    // Custom toolbar items
//    AGIPCToolbarItem *selectAll = [[AGIPCToolbarItem alloc] initWithBarButtonItem:[[UIBarButtonItem alloc] initWithTitle:@"+ Select All" style:UIBarButtonItemStyleBordered target:nil action:nil] andSelectionBlock:^BOOL(NSUInteger index, ALAsset *asset) {
//        return YES;
//    }];
//    AGIPCToolbarItem *flexible = [[AGIPCToolbarItem alloc] initWithBarButtonItem:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] andSelectionBlock:nil];
//    AGIPCToolbarItem *selectOdd = [[AGIPCToolbarItem alloc] initWithBarButtonItem:[[UIBarButtonItem alloc] initWithTitle:@"+ Select Odd" style:UIBarButtonItemStyleBordered target:nil action:nil] andSelectionBlock:^BOOL(NSUInteger index, ALAsset *asset) {
//        return !(index % 2);
//    }];
//    AGIPCToolbarItem *deselectAll = [[AGIPCToolbarItem alloc] initWithBarButtonItem:[[UIBarButtonItem alloc] initWithTitle:@"- Deselect All" style:UIBarButtonItemStyleBordered target:nil action:nil] andSelectionBlock:^BOOL(NSUInteger index, ALAsset *asset) {
//        return NO;
//    }];
//    //    ipc.toolbarItemsForManagingTheSelection = @[selectAll, flexible, selectOdd, flexible, deselectAll];
//    //    imagePickerController.toolbarItemsForManagingTheSelection = [NSArray array];
//    
//    //    imagePickerController.maximumNumberOfPhotos = 3;
//    [self presentModalViewController:ipc animated:YES];
    
}



//MARS serverButton的响应事件处理方法
- (void)severButtonPressed:(id)sender {
    //MARS 修改RootView的方法
    //    UIWindow *widow = [[[UIApplication sharedApplication] delegate] window];
    //    widow.rootViewController
    DDSeverViewController *sever = [[DDSeverViewController alloc] initWithNibName:nil bundle:nil];
    [self presentViewController:sever animated:NO completion:nil];
    [sever release];
}

- (void)backbuttonPressed:(id)sender {
    [DDVerifyThread closeAndClead];
    [DDTCPListenThread closeAndClear];
    DDLoginViewController* loginView = [[DDLoginViewController alloc]init];
    [self presentViewController:loginView animated:NO completion:nil];
    [loginView release];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
